Let’s Git Started
Installation
Git installation: Windows
- For using Git/Github and Unix, install
git bashfor windows from:
- Configure git, your name and email address
- In VScode (Positron)
- Open command pallete with
Ctrl + Shift + P - Type “Terminal:Select Default Profile”
- Choose Git Bash for your default terminal
Git for macOS
- Check if Xcode command line tools is not installed (if installed, skip)
- Open terminal
- Configure git, your name and email address
Note
You should also create an account on GitHub. Optionally, register with USF for a student previleges.
Check if terminal understands
gitcommand. Type git on terminal and confirm no error message.
Github Login
Make sure you are able to login to Github.
Introduction to Git/GitHub
Why do we need Git?
Git
Distributed version control system, created by Linus Torvalds (2005)
“Track changes” made by team members, merge into main, etc.
Considered a “must” for software development, and many data science projects.
Has a learning curve, but it’s worth learning even for solo projects.
GitHub
An online hosting platform that is based on Git system
Easy to browse other repositories (public, free)
Others: GitLab, Bitbucket, GitBucket, etc…
Why Git?
Version control: track changes, revert to previous versions
Collaboration: multiple people can work on the same project
Backup: store your project on the cloud
Portfolio: showcase your work to potential employers
Open source: contribute to other projects
Is Git same as GitHub?
Nope.
Git can be done completely locally (w/o internet).
GitHub deploys Git to online cloud system.
Does Git/Github work with any files?
Git is primarily designed to handle text files and tracks changes line by line.
It can upload binary files (e.g. images, pdfs), but doesn’t track differences like text files.
GitHub has a small file size limit (100MB)
It is intended for code tracking. Large data should be stored elsewhere.
Git Terminology/workflow
Why learn Git in this course?
On top of its practicality, it gives you hands-on experience & knowledge of cryptographic Merkle Tree algorithm.
- Understand underlying mechanism of Blockchains
Blockchains:
- Consensus algorithm (decentralized)
- Merkle tree
Git shell commands
Git init
git init is used to initialize a new git repository from current working directory.
- This command creates a new hidden subdirectory named .git
- If you want to stop git, just remove this folder.
Git status
git status shows the status of changes as untracked, modified, or staged.
Understanding Git Status
git status provides information about the state of your working directory and staging area. Here are the key terms:
Untracked files: Files that Git isn’t tracking yet.
Tracked files: Files that Git has been monitoring for changes.
- Staged: Files added to the staging area (using
git add). - Unstaged: Modifications made to tracked files that haven’t been staged yet.
- Includes “Modified(M)” and “Deleted(D)”
- Staged: Files added to the staging area (using
Git log
git log shows the history of commits and its corresponding hashes.
:qto quit!
Stage files
git add stages files for commit.
Git reset (Unstaging)
git reset unstages all files and folders you added.
Make commit
git commit creates your project’s version, or a hash block.
You should provide message folling -m, that describes the version.
- Consider committing a serious process of your job, like signing a doc.
Exercise
1) My First Commit in Git
- Make class folder tracked by git (FIN4770)
Go to your folder from terminal.
In the
Shellfolder, create a text fileFor_first_commit.sh.In the file, add text “Learing Git is Fun!”
Get out of
Shellfolder, and back to your class folder.Now
initiate git in your class folder.
- After initializing, git now “scans” your folder.
- Check the status of git, with
git status. What do you see?
- Let’s add all of the folders and files be tracked by git.
- Use
git add .to stage all untracked files and folders. - From now on, all those files are tracked.
Now let’s make a commit.
- By committing, you are making a version of the project.
- You can come back to this state of your project in the future.
- Make informative message in the commit!
For commit message, use “My first commit”.
- Check the status of git once again.
Git reset 2
git reset also can reset your previous commit.
- You should mention the hash (id) of commit.
HEADis a pointer where you are currently at.HEAD~1means 1 commit before current hash (version).
git reset B
- Changes made after B are “unstaged”
- Go to the status where “Before I staged files and folders”
- Same as
git reset --mixed B
git reset --soft B
- Changes after B are “staged”
- Go to the status where I was “Ready to commit again”
git reset --hard B
- Match completely to commit B, all others discarded.
- Go to the status when I just finished committing B.
- Be careful!! all changes after that are gone.
Exercise
Second commit & Reset
- Let’s make a faulty second commit then revert:
Go to your folder from terminal.
In the Shell folder, edit
For_first_commit.shfile:- Append line: “Learning Git is Not Fun at all!”
Get out of Shell folder, and back to your class folder.
Then check the status. What do you see?
Remember this status. We will come back to here after reset.
Add all changes to the staging area.
Commit with message “Test second commit”
- This is now your second version.
Check the status.
Check the commit logs with
git logorgit log --oneline.
- Now let’s get back to the previous version (Q1).
- Run
git reset HEAD~1 - or
git reset (hash)
- Check the status. Can you tell where you are?
- Let’s do the faulty second commit once again.
- First add the change to the staging area
git add . - Commit with message “Yet another test commit”
- Check status and log to see where you are.
- Make sure you are on the second commit.
- Let’s hard reset to status when we first committed.
- We will remove all the changes made after the first commit.
- Run
git reset --hard HEAD~1. - Check the status and log. Confirm that your faulty edit is gone!
Set Remote repository
The process so far is only for your local version management, which is also completely fine for local work.
However, you can also make your repository “Synchronized” to a cloud server: Github.
git remote add adds remote repository.
originis naming convention for Github remote repo.mainis your local branch name.
Let’s check the branch names first.
Exercise: Check branch
Let’s check the current local branch name. We use main branch named as “main”.
If your local branch name is not main (i.e. master or etc), change it to main with below command.
Add remote repository
Now we can add remote repository with following command.
Exercise
2) My first push to GitHub
- Create your remote repository on Github website:
Go to the github website
Choose visibility to public
Name your repository nicely for this classroom
DO NOT ADD README FILE (leave it unchecked)
Copy the address to a text file
- The address will be like https://github.com/YourGithubId/YourRepoName.git
- Add your remote repository address as “origin” branch
From now on, you can browse your remote branches with
Push to repository
git push sends your commit to the remote repository.
Exercise
Let’s push your local classrom git to the github.
Check first: 1) Your local status is clean (check with git status) 2) If not clean (You made edit after the initial version) stage changes and commit.
Then type below to push:
Homework
Browse your github site, and see files are uploaded.
https://github.com/yourGithubId/Reponame.git
- Share the address to me.
By default, git will only track files, not empty folders. Folders are tracked when files are in it.
Git clone
What if you wanted to download someone else’s remote repository to your local machine? Git clone is for the first time downloading from the remote repository.
Exercise
Now let’s clone OUR CLASS repository (that I upload all class materials).
git clone action will create a folder and download everything from the upstream.
- You should make a separate folder apart from your own class folder
- Move one folder up from your class folder when you clone it.
Example: if your class folder address is ~/my_fin4770 then you should clone it from ~.
Pull from repository
git pull downloads and apply changes from the repote repository.
Actually, git pull does two things altogether:
- fetch: download changes, but not apply the changes to local
- If you want to see the differences between the two.
- merge: apply changes to your local file
In-class Exercise
Now, I will make modify and update the class repositroy.
After the update, use git pull from the local class repo to apply the update.
Github and IDE
When using github from other apps (e.g. Positron, VScode, etc), you’ll use PAT (personal access token) instead of password.
Generate token from the github page, possibly with no expiration (or long enough)
- Copy the token to clipboard (you won’t see the token again)
- Store it somewhere temporarily
More Git concepts
Three pull method
By default, git pull is
git pull has three methods:
git pull --merge(default)git pull --rebasegit pull --ff-only
git merge
Combines changes into a new merge commit. You will need to combine and resolve conflicts.
- B, C are your local change
Example: Git merge scenario
Content of file.txt
Alice edits the second line and commits and push to main first:
Bob also edits the same line, but did not pull Alice’s changes. He edits and commits locally:
When Bob tries to push changes with git push, Git will reject because his local branch is behind the remote.
Bob runs git pull which is git fetch and git merge: git shows the conflicts
The file.txt now contains conflict markers
To resolve conflict, Bob should modify the file manually.
Bob stages this file and commits the resolution:
After resolving, Bob pushes with git push.
git rebase
Git replays your local branch on top of the remote branch, creating a linear history:
Example: Git rebase scenario
Content of file.txt
Alice edits the second line and commits and push to main first:
Bob also edits the same line, but did not pull Alice’s changes. He edits and commits locally:
When Bob tries to push changes with git push, Git will reject because his local branch is behind the remote.
Bob runs git pull --rebase which is git fetch and git rebase:
Instead of creating a merge commit,
git rewinds Bob’s local commit C
applies Alice’s changes from remote (origin/main) B
reapplies C on top of B
When there’s no conflict, (i.e., they did not change the same line of code) git pull –-rebase won’t raise any conflict message.
However, in our scenario, there’s conflict since both modified the same line, so:
rebase is halted
should be continued after conflict resolution
Similar to merge, conflict should be resolved manually.
The file.txt now contains conflict markers
To resolve conflict, Bob should modify the file manually.
Bob stages this file and continues the rebase (no commit)
After that Bob pushes with git push. The history is linear.
git merge --ff-only
Used when you expect no conflicts.
When you want linear history and want to move your local branch pointer forward without modifying.
- Git rejects if there is any conflict
.gitignore
Git keep tracks all changes within the project directory
New file, folder
Modifications (changes or updates)
Deletion
If there are files/folder you don’t want to track, specify them in .gitignore file.
git clean
If you want to remove untracked (not unstaged!) files: